package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.ImageShow;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.mybase.BaseApp;
import com.lsy.weizhi2.utils.MyMethod;

/**
 * Created by 嗨哟 on 2017/5/12.
 */

public class ImageAdapterBig extends PagerAdapter {
    private Context context;
    private int[]str;
    private LayoutInflater layoutInflater;

    public ImageAdapterBig(Context context, int[] str) {
        this.context = context;
        this.str = str;
    }

    @Override
    public int getCount() {
        return str==null?1:str.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        imageView.setAdjustViewBounds(true);
        RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        imageView.setLayoutParams(layoutParams);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        if(str==null||str.length==0){
            imageView.setImageResource(R.mipmap.nopic);
        }else{
            if(str[0]>1000000){
                imageView.setImageResource(str[position]);
            }else{
                Glide.with(context)
                        .load(new GlideUrl(Constants.DOWN_USER_PICTURE+str[position],new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID",MyInfo.User_Id+"").build()))
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(imageView);
            }
        }
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[]images=new String[str.length+1];
                int[]strs=new int[str.length+1];
                for (int i = 0; i < str.length; i++) {
                    images[i]="image"+i;
                    strs[i]=str[i];
                }
                images[str.length]="num";
                strs[str.length]=str.length;
                MyMethod.toActivity(context, ImageShow.class,images,strs);
            }
        });
        ((ViewPager) container).addView(imageView);
        return imageView;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ImageView) object);
    }
}
